package com.ruoyi.postulant.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.postulant.domain.Apply;
import com.ruoyi.postulant.service.IApplyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动报名Controller
 *
 * @author wanghong
 * @date 2025-03-15
 */
@RestController
@RequestMapping("/postulant/apply")
public class ApplyController extends BaseController
{
    @Autowired
    private IApplyService applyService;

    /**
     * 查询活动报名列表
     */
    @PreAuthorize("@ss.hasPermi('postulant:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(Apply apply)
    {
        Long userId = SecurityUtils.getUserId();
        if(userId > 1){
            apply.setUserId(Math.toIntExact(userId));
        }
        startPage();
        List<Apply> list = applyService.selectApplyList(apply);
        return getDataTable(list);
    }

    /**
     * 导出活动报名列表
     */
    @PreAuthorize("@ss.hasPermi('postulant:apply:export')")
    @Log(title = "活动报名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Apply apply)
    {
        List<Apply> list = applyService.selectApplyList(apply);
        ExcelUtil<Apply> util = new ExcelUtil<Apply>(Apply.class);
        util.exportExcel(response, list, "活动报名数据");
    }

    /**
     * 获取活动报名详细信息
     */
    @PreAuthorize("@ss.hasPermi('postulant:apply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(applyService.selectApplyById(id));
    }

    /**
     * 新增活动报名
     */
    @Log(title = "活动报名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Apply apply)
    {
        Long userId = SecurityUtils.getUserId();
        long count = applyService.count(new QueryWrapper<Apply>().eq("activity_id",apply.getActivityId()).eq("user_id", userId));
        if (count > 0) {
            return AjaxResult.error("请勿重复报名");
        }
        apply.setUserId(Math.toIntExact(userId));
        return toAjax(applyService.insertApply(apply));
    }

    /**
     * 修改活动报名
     */
    @PreAuthorize("@ss.hasPermi('postulant:apply:edit')")
    @Log(title = "活动报名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Apply apply)
    {
        return toAjax(applyService.updateApply(apply));
    }

    /**
     * 删除活动报名
     */
    @PreAuthorize("@ss.hasPermi('postulant:apply:remove')")
    @Log(title = "活动报名", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(applyService.deleteApplyByIds(ids));
    }
}
